package com.photoeditor.demo.model.store.listener;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import com.photoeditor.demo.ui.activity.HomeActivity;
import com.photoeditor.demo.ui.widget.image.MediaEditSupportType;

import java.util.Stack;

/**
 * Created by dell on 2017/10/26.
 */

public class ApplyActivityListener implements Application.ActivityLifecycleCallbacks {

    /**
     * 应用的主界面
     */
    public static final Class<?> MainClass = HomeActivity.class;

    private Stack<MediaEditSupportType> applyActivityStack = new Stack<>();

    private boolean hasMainClass = false;

    private static ApplyActivityListener instance;

    public static ApplyActivityListener getInstance() {
        if (instance == null) {
            synchronized (ApplyActivityListener.class) {
                if (instance == null) {
                    instance = new ApplyActivityListener();
                }
            }
        }
        return instance;
    }

    public Class<?> getApplyClass(int type) {
        if (applyActivityStack.isEmpty()) {
            return null;
        } else {
            MediaEditSupportType mediaEditSupportType = applyActivityStack.peek();
            if (mediaEditSupportType.isSupport(type)) {
                return mediaEditSupportType.getClass();
            } else {
                if (mediaEditSupportType instanceof Activity) {
                    ((Activity) mediaEditSupportType).finish();
                }
                return null;
            }
        }
    }

    private void checkAndPushActivity(Activity activity) {
        synchronized (ApplyActivityListener.this) {
            if (activity instanceof MediaEditSupportType) {
                applyActivityStack.push((MediaEditSupportType) activity);
            }
        }
    }

    private void checkAndPopActivity(Activity activity) {
        synchronized (ApplyActivityListener.this) {
            if (activity instanceof MediaEditSupportType && !applyActivityStack.isEmpty()) {
                applyActivityStack.remove(activity);
            }
        }
    }

    public boolean isHasMainClass() {
        return hasMainClass;
    }

    private void checkMainClass(String className, boolean isOpen) {
        if (TextUtils.equals(className, MainClass.getName())) {
            hasMainClass = isOpen;
        }
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        checkAndPushActivity(activity);
        checkMainClass(activity.getClass().getName(), true);
    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        checkAndPopActivity(activity);
        checkMainClass(activity.getClass().getName(), false);
    }
}
